import { createApp } from "vue";
import Vant from "vant";
import mitt from "mitt";
import "vant/lib/index.css";
import router from "@/router";
import App from "@/App";
import store from "@/store";
import socket from "./socket";


socket.on("connect", () => {
  console.log("客户端链接成功", socket.id);    
});  
 
const app = createApp(App);

app.$bus = mitt();

const state = store.state;
 
router.beforeEach(async (to, from, next) => {
  const token = state.user.token;
  const userInfo = JSON.stringify(state.user.userInfo);
  if (
    (token === "" || userInfo === "{}") &&
    to.path !== "/login" &&
    to.path !== "/register"
  ) {
    return router.replace({
      path: "/login",
    });
  } else {
    if (token !== "" && userInfo !== "{}") {
      if (to.path === "/login") {
        router.replace({
          path: "/",
        });
      }
    }
  }
  next();
});

router.afterEach((to, from) => {
  document.title = to.meta.title ? to.meta.title : "消息";
});

app.use(Vant).use(store).use(router).mount("#app");

app.config.globalProperties.$socket = socket;
